/*
 * Copyright 2015-2016 Tumblr, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tumblr.backboard.example.slice;

import com.tumblr.backboard.example.AppearFragment;
import com.tumblr.backboard.example.BloomFragment;
import com.tumblr.backboard.example.ConstrainedFragment;
import com.tumblr.backboard.example.ExplosionFragment;
import com.tumblr.backboard.example.FlowerFragment;
import com.tumblr.backboard.example.FollowFragment;
import com.tumblr.backboard.example.MoveFragment;
import com.tumblr.backboard.example.PopupDialogMenu;
import com.tumblr.backboard.example.PressFragment;
import com.tumblr.backboard.example.ResourceTable;
import com.tumblr.backboard.example.ScaleFragment;
import com.tumblr.backboard.example.SnapFragment;
import com.tumblr.backboard.example.ZoomFragment;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;

/**
 * 文件描述
 *
 * @author ericleong
 * @since 2021-05-12
 */
public class BackboardAbilitySlice extends AbilitySlice {
    private static final int WIDTH_DIALOG = 100;

    private static final int HEIGHT_DIALOG = 470;

    private static final int WIDTH_ARROW = 20;

    private static final int HEIGHT_ARROW = 20;

    private static final int RADIUS = 20;

    private PopupDialogMenu mPopupDialogMenu;

    private Text mTitleText;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_backboard);
        ((FractionAbility) getAbility()).getFractionManager().startFractionScheduler().add(ResourceTable.Id_stack,
                new MoveFragment()).submit();
        initView();
    }

    private void initView() {
        mTitleText = (Text) findComponentById(ResourceTable.Id_title_text);
        findComponentById(ResourceTable.Id_title_menu).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Component anchorView = findComponentById(ResourceTable.Id_title_menu);
                Component componentMenu =
                        LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_menu_popup_dialog, null,
                                false);
                showTransformersTip(anchorView, componentMenu);
            }
        });
    }

    private void showTransformersTip(Component anchorView, Component component) {
        Component.ClickedListener clickedListener = new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                mPopupDialogMenu.destroy();
                componentClick(component.getId());
            }
        };

        mPopupDialogMenu = new PopupDialogMenu(anchorView, component) {
            @Override
            public boolean isTouchOutside() {
                // 点击浮窗中自定按钮关闭浮窗
                dismissTip();
                return false;
            }

            @Override
            public void initView(Component contentView) {
                contentView.findComponentById(ResourceTable.Id_action_move).setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_snap).setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_scale).setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_bloom).setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_flower).setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_appear).setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_explosion).setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_follow).setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_zoom).setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_constrained).setClickedListener(clickedListener);
                contentView.findComponentById(ResourceTable.Id_action_press).setClickedListener(clickedListener);
            }
        };
        mPopupDialogMenu.setSize(AttrHelper.vp2px(WIDTH_DIALOG, getContext()),
                AttrHelper.vp2px(HEIGHT_DIALOG, getContext()));
        mPopupDialogMenu.setMode(LayoutAlignment.BOTTOM);
        mPopupDialogMenu.setBgColor(new Color(Color.getIntColor("#00EEEEEE")));
        mPopupDialogMenu.setHasArrow(true);
        mPopupDialogMenu.setCornerRadius(RADIUS);
        mPopupDialogMenu.setAutoClosable(true);
        mPopupDialogMenu.setTransparent(true);
        mPopupDialogMenu.setArrowSize(WIDTH_ARROW, HEIGHT_ARROW);
        mPopupDialogMenu.showDialog();
    }

    private void componentClick(int id) {
        Fraction fraction = new MoveFragment();
        switch (id) {
            case ResourceTable.Id_action_snap:
                mTitleText.setText(ResourceTable.String_action_snap);
                fraction = new SnapFragment();
                break;
            case ResourceTable.Id_action_scale:
                mTitleText.setText(ResourceTable.String_action_scale);
                fraction = new ScaleFragment();
                break;
            case ResourceTable.Id_action_flower:
                mTitleText.setText(ResourceTable.String_action_flower);
                fraction = new FlowerFragment();
                break;
            case ResourceTable.Id_action_bloom:
                mTitleText.setText(ResourceTable.String_action_bloom);
                fraction = new BloomFragment();
                break;
            case ResourceTable.Id_action_appear:
                mTitleText.setText(ResourceTable.String_action_appear);
                fraction = new AppearFragment();
                break;
            case ResourceTable.Id_action_explosion:
                mTitleText.setText(ResourceTable.String_action_explosion);
                fraction = new ExplosionFragment();
                break;
            case ResourceTable.Id_action_follow:
                mTitleText.setText(ResourceTable.String_action_follow);
                fraction = new FollowFragment();
                break;
            case ResourceTable.Id_action_zoom:
                mTitleText.setText(ResourceTable.String_action_zoom);
                fraction = new ZoomFragment();
                break;
            case ResourceTable.Id_action_constrained:
                mTitleText.setText(ResourceTable.String_action_constrained);
                fraction = new ConstrainedFragment();
                break;
            case ResourceTable.Id_action_press:
                mTitleText.setText(ResourceTable.String_action_press);
                fraction = new PressFragment();
                break;
            default:
                break;
        }
        ((FractionAbility) getAbility()).getFractionManager().startFractionScheduler()
                .replace(ResourceTable.Id_stack, fraction).submit();
    }
}
